package com.key.service.impl;

import com.github.pagehelper.PageHelper;
import com.key.common.R;
import com.key.common.dto.AssetsFindDTO;
import com.key.common.dto.ReceiptAssetsDTO;
import com.key.common.dto.ReceiptDTO;
import com.key.common.vo.AssetsTVO;
import com.key.entity.*;
import com.key.mapper.*;
import com.key.service.ApprovalService;
import com.key.service.ReceiptAssetsService;
import com.key.utils.OddNumbersUtils;
import com.key.utils.TranException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author wen
 * @create 2023-02-20 10:10
 */
@Service
public class ReceiptAssetsServiceImpl implements ReceiptAssetsService {

    @Autowired
    ReceiptAssetsMapper receiptAssetsMapper;
    @Autowired
    AssetsMapper assetsMapper;
    @Autowired
    ApplyMapper applyMapper;

    @Autowired
    ApprovalMapper approvalMapper;

    @Autowired
    IndexMapper indexMapper;

    @Autowired
    IndexAssetsMapper indexAssetsMapper;

    @Override
    public R findReceiptAssets(ReceiptDTO receiptDTO) {
        int currentPage=receiptDTO.getCurrentPage();
        int pageSize=receiptDTO.getPageSize();
        R r=new R();

        List<ReceiptAssets> receiptAssetsList2= receiptAssetsMapper.selectReceipt(receiptDTO);

        PageHelper.startPage(currentPage,pageSize);
        List<ReceiptAssets> receiptAssetsList= receiptAssetsMapper.selectReceipt(receiptDTO);
        if(receiptAssetsList!=null){
            r.setCode(200);
            r.setCount(receiptAssetsList2.size());
            r.setMsg("success");
            r.setData(receiptAssetsList);
        }else {
            r.setCode(400);
            r.setMsg("查询失败！");
        }
        return r;
    }

    //依据多个资产生成新的领用订单
    @Override
    public R creatReceipt(List<Long> assetsCodes, ReceiptAssets receiptAssets) {
        R r=new R();
        String receiptNumber=OddNumbersUtils.creatON("LY");
        Assets assets=new Assets();
        assets.setAssetsState(2);


        for (long i:assetsCodes) {
            receiptAssetsMapper.insertReceiptAndAssets(receiptNumber,i);
            assets.setAssetsId(i);
            assetsMapper.updateAssets(assets);
        }
        receiptAssets.setState("待审批");
        receiptAssets.setApproveProgress("发起");
        receiptAssets.setReceiptDate(new Timestamp(System.currentTimeMillis()));
        receiptAssets.setReceiptNumber(receiptNumber);

        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        String s = String.valueOf(subject.getPrincipal());
        if(s==null||s==""){
            s="张三";
        }
        receiptAssets.setReceipter(s);
        int result=receiptAssetsMapper.insertReceipt(receiptAssets);

        //添加申请表
        Apply apply=new Apply();
        apply.setApplyNumber(receiptNumber);
        apply.setAssetNum(assetsCodes.size());
        apply.setApprover("待完善（该位置creatReceipt）");
        apply.setApplyDate(receiptAssets.getReceiptDate());
        apply.setStats("审批中");
        apply.setApplyType("资产领用");
        int i= applyMapper.insertApply(apply);

        //领用申请审批单添加
        SysUser sysUser= indexMapper.selectUserInfoByUserName(s);
        Company company = indexAssetsMapper.selectAssetsInCompany(sysUser.getUserId());
        Department department = indexAssetsMapper.selectDeptInfoByEpmId(sysUser.getUserId());
        Approval approval = new Approval();
        approval.setState("未审批");
        approval.setApprovalNo(receiptNumber);
        approval.setApplicationType("领用");
        approval.setApplicationTime(OddNumbersUtils.getDay());
        approval.setApplicationPerson(s);
        approval.setApplicationCompany(company.getCompanyName());
        approval.setApplicationDepartment(department.getDepartmentName());

        approvalMapper.insertApproval(approval);


        if(result!=0){
            r.setCode(200);
            r.setMsg("生成单据成功！领用申请已发送");
        }else {
            r.setCode(500);
            r.setMsg("系统错误");
        }
        return r;
    }

    @Override
    @Transactional
    public R deleteReceiptAssets(String receiptNumber) {
        R r=new R();
        try {
            int i= receiptAssetsMapper.deleteReceiptAssetsByReceiptNumber(receiptNumber);
            int j=receiptAssetsMapper.updateReceiptAndAssets(receiptNumber);
            r.setCode(200);
            r.setMsg("删除成功");
        }catch (Exception e){
            throw new TranException("事务错误");
        }
        return r;
    }

    @Override
    public R editReceiptAssets(ReceiptDTO receiptDTO) {
        R r=new R();
        int i=receiptAssetsMapper.updateReceiptAssets(receiptDTO);
        if(i!=0){
            r.setCode(200);
            r.setMsg("修改成功！");
        }else {
            r.setCode(300);
            r.setMsg("修改失败！");
        }
        return r;
    }

    //查询可退库的领用单
    @Override
    public List<ReceiptAssets> findCanWithdrawalReceiptAssets() {

        List<ReceiptAssets> resultList=receiptAssetsMapper.selectCanWithdrawalReceiptAssets();
        return resultList;
    }

    @Override
    public List<Assets> findAssetsByReceiptAssets(ReceiptAssets receiptAssets) {
        List<Assets> resultList=receiptAssetsMapper.selectAssetsByReceiptAssetsNumber(receiptAssets.getReceiptNumber());
        return resultList;
    }

    //查询该领用单号下的资产
    @Override
    public List<AssetsTVO> findAssetsTVOByReceiptAssets(ReceiptAssets receiptAssets) {
        List<AssetsTVO> resultList=receiptAssetsMapper.selectAssetsTVOByReceiptAssetsNumber(receiptAssets.getReceiptNumber());
        return resultList;
    }

}
